/*
 * @(#)AuthService.java 2009-2-19
 * Copyright (c) 2008-2015 by CJK
 * All rights reserved.
 */
package net.beyond.core.service;

import java.util.List;

import net.beyond.common.ssh.GenericService;
import net.beyond.core.CmsException;
import net.beyond.core.entity.Group;
import net.beyond.core.entity.Module;
import net.beyond.core.entity.Resource;
import net.beyond.core.entity.Role;
import net.beyond.core.entity.User;
import net.beyond.core.keys.ExceptionKeys;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * CoreService 的实现
 * 
 * @author Shmiluyu
 * @version 1.000 2009-2-19
 */
@Service
@Transactional
public class CoreServiceImpl implements CoreService {

	private static final long serialVersionUID = 1L;
	@javax.annotation.Resource
	private GenericService<User> userService;
	@javax.annotation.Resource
	private GenericService<Group> groupService;
	@javax.annotation.Resource
	private GenericService<Resource> resService;
	@javax.annotation.Resource
	private GenericService<Role> roleService;
	@javax.annotation.Resource
	private GenericService<Module> modService;

	/*
	 * @see
	 * net.beyond.core.auth.AuthService#registerUser(net.beyond.core.auth.entity
	 * .User)
	 */
	public User registerUser(User newUser) {
		if (checkUsernameExists(newUser.getUsername()) == true) {
			throw new CmsException(ExceptionKeys.USERNAME_EXISTS);
		}
		return userService.save(newUser);
	}

	/*
	 * @see
	 * net.beyond.core.auth.AuthService#validateUser(net.beyond.core.auth.entity
	 * .User)
	 */
	@SuppressWarnings("unchecked")
	public User validateUser(User user) {
		String hql = "from User where username=:username and password=:password";
		List<User> results = userService.getDao().getHibTpl().findByValueBean(
				hql, user);
		if (results != null && results.size() > 0) {
			return results.get(0);
		} else {
			return null;
		}
	}

	/*
	 * @see
	 * net.beyond.core.auth.AuthService#checkUsernameExists(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public boolean checkUsernameExists(String username) {
		String hql = "from User where username=?";
		List<User> results = userService.getDao().getHibTpl().find(hql,
				username);
		if (results != null && results.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	/*
	 * @see
	 * net.beyond.core.service.CoreService#getModuleOfUser(net.beyond.core.entity
	 * .User)
	 */
	@SuppressWarnings("unchecked")
	public List<Module> getModuleOfUser(User user) {
		String hql = "select module from Module as module, User as user, Resource as res where res.module=module  and res in elements(user.roles.resources)  and user.username=?";
		return modService.getDao().getHibTpl().find(hql, user.getUsername());
	}

	/*
	 * @see
	 * net.beyond.core.service.CoreService#getRootResourceOfModule(net.beyond
	 * .core.entity.Module)
	 */
	@SuppressWarnings("unchecked")
	public List<Resource> getRootResourceOfModuleForMenu(Module module) {
		String hql = "from Resource as res where res.module.id=? and res.parent is null";
		return resService.getDao().getHibTpl().find(hql, module.getId());
	}

	/*
	 * @see
	 * net.beyond.core.service.CoreService#getChildrenOfResource(net.beyond.
	 * core.entity.Resource)
	 */
	@SuppressWarnings("unchecked")
	public List<Resource> getChildrenOfResource(Resource parent, int resType) {
		String hql = "from Resource as res where res.parent.id=? and (res.type=? or res.type=0)";
		return resService.getDao().getHibTpl().find(hql,
				new Object[] { parent.getId(), resType });
	}

	public GenericService<User> getUserService() {
		return userService;
	}

	public void setUserService(GenericService<User> userService) {
		this.userService = userService;
	}

	public GenericService<Group> getGroupService() {
		return groupService;
	}

	public void setGroupService(GenericService<Group> groupService) {
		this.groupService = groupService;
	}

	public GenericService<Resource> getResService() {
		return resService;
	}

	public void setResService(GenericService<Resource> resService) {
		this.resService = resService;
	}

	public GenericService<Role> getRoleService() {
		return roleService;
	}

	public void setRoleService(GenericService<Role> roleService) {
		this.roleService = roleService;
	}

	public GenericService<Module> getModService() {
		return modService;
	}

	public void setModService(GenericService<Module> modService) {
		this.modService = modService;
	}

}